var Menu = JW.Observable.extend({
    init: function()
        {  
            var MenuWindow = $('<div class="MenuWindow"/>');
            $("body").append(MenuWindow);
            var AppendField = 'MenuWindow';
            //----------------------------------------------
            var NewGameButton = new Button({
                        top    :400,
                        left   :570,
                        width  :200,
                        height :41,
						text   :"New game"
                        });  
            var NewGameButtonView = new MenuButton(NewGameButton,AppendField);
            var MakersButton = new Button({
                        top    :450,
                        left   :570,
                        width  :200,
                        height :41,
                        text   :"Makers"
                        }); 
            var MakersButtonView = new MenuButton(MakersButton,AppendField);
            var ExitButton = new Button({
                        top    :500,
                        left   :570,
                        width  :200,
                        height :41,
                        text   :"Exit"
                        }); 
			var ExitButtonView = new MenuButton(ExitButton,AppendField);
            var NewGameController;
            var GameField;
            var ScoreField;
			$(".New")   .click(this.clickNewGame.inScope(this));
            $(".Makers").click(this.clickMakers .inScope(this));
            $(".Exit")  .click(this.clickExit   .inScope(this));
        },
		clickNewGame: function()
		{
			this.pauseHandler = this.onPause.inScope(this);
			$("body").keydown(this.pauseHandler);
			$(".MenuButton").css({visibility: "hidden"});  
			$(".MenuWindow").css({visibility: "hidden"}); 
			GameField = $('<div class="GameField"></>');
			$("body").append(GameField);
			ScoreField = $('<div class="ScoreField"></>');
			$("body").append(ScoreField);
			NewGameController = new GameController();
			NewGameController.GameObjects.bind("gameend", this.OnGameEnd, this);
			NewGameController.GameObjects.bind("Lvl1Clear", this.OnLvl1Clear, this);
		},	   
        onPause: function(event)
		{
			var key = event.keyCode;
			if (key == 27)
			{
				if (!this.pauseWindow)
				{
					this.pauseWindow=$("<div class='pauseWindow' />")
					$(".GameField").append(this.pauseWindow);
					var pauseWindowBackToMenu = new Button
					({
						   top:283,
						   left:360,
						   width:80,
						   height:35,
						   text:"Menu"
					}); 
					var pauseWindowBackToMenuView = new MenuButton(pauseWindowBackToMenu,"pauseWindow");
					$(".Menu").addClass("pauseWindowBackToMenu");
					clearInterval(NewGameController.Timer);
					$(".pauseWindowBackToMenu").click((function()
							{
								ScoreField.remove();
								this.pauseWindow.remove();
								delete this.pauseWindow;
								NewGameController.destroy();
								pauseWindowBackToMenuView.destroy();
								GameField.remove();
								$(".MenuButton").css({visibility: "visible"});  
								$(".MenuWindow").css({visibility: "visible"}); 
								$("body").unbind("keydown", this.pauseHandler);
							}).inScope(this));			
				}	
				else
				{
				NewGameController.Timer      = setInterval(NewGameController.OnTimer     .inScope(NewGameController),  30);
				this.pauseWindow.remove();
				delete this.pauseWindow;
				}
			}
		},
		clickMakers: function()
		{
			var MakersList = $('<div class="MakersList"> \
														Powered by Displayers<br></br> \
														Sibert Dmitry & Kaunov Vladimir <br></br>   \
														<div class="MakersListClose onTouch"> Click To Close </> \
														</>')
			$(".MenuWindow").append(MakersList)
			$(".MakersList").fadeOut(0).fadeIn(1000);
			$(".MakersList").click(function(){MakersList.remove()})

		},
          //-----------------------------------------------------------------------|
        clickExit: function()
        {
			alert('THIS IS BbIXO6')
			//self.close();      
        },
		OnGameEnd: function(event)
		{
            $("body").unbind("keydown", this.pauseHandler);
			var scoreList = $('<div class="scoreList"> \
														Game Over!       \
														You have:<br></br>\
														'+NewGameController.ScoreBar.status+' scores!<br></br>   \
														<div class="MakersListClose onTouch"> Click To Close </> \
														</>')
			$(".GameField").append(scoreList);
			$(".scoreList").fadeOut(0).fadeIn(1000);
			$(".scoreList").click(function(){scoreList.remove()})
		},
        OnLvl1Clear: function (event) {
            $("body").unbind("keydown", this.pauseHandler);
            var scoreList = $('<div class="scoreList"> \
														        Lvl1 Cleared! (Gratz, you won)<br></br>      \
														        You have:\
														        ' + NewGameController.ScoreBar.status + ' scores!<br></br>   \
														        <div class="MakersListClose onTouch"> Click To Close </> \
														        </>')
            $(".GameField").append(scoreList);
            $(".scoreList").fadeOut(0).fadeIn(500);
            $(".scoreList").click(function () { scoreList.remove() })
        }
});